/* Hoang Quang Hieu
 * K15-CMU-TPM
 * 
 */

package cs;
import java.util.*;
/**
 *
 * @author QuangHieu
 */
public class Main {

    public static boolean NguyenTo(int a)
    {
        if(a == 2 || a == 3)
            return true;
        if (a == 1|| a % 2 == 0||a % 3 == 0)
            return false;
        int m = (int) Math.sqrt(a);
        int y=2;
        for(int i=5;i<=m;i=i+y, y = 6 - y)
            if(a % i == 0)
                return false;
        return true;
    }

    public static void sort(int m[], int n){
        for(int i=0;i<n-1;i++)
            for(int j=i;j<n;j++)
                if(m[i]<m[j]){
                    int a=m[j];
                    m[j]=m[i];
                    m[i]=a;
                }
        for(int i=0;i<n;i++)
            System.out.print(m[i]+" ");
    }

    public static boolean search(int m[],int n, int x){
        sort(m,n);
        int l=0, r=n-1;
        while(l<=r){
            int b=(l+r)/2;
            if(m[b]==x){
                System.out.print("\nTrue\n");
                return true;
            }
            else if(m[b]>x)
                r=b-1;
            else l=b+1;
        }
        System.out.print("\nFalse\n");
        return false;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int a=8000000; int count=0;
        //int m[] = new int[a];
        int t1,t2,t3;
        //int x=(int)(Math.random()*1000000);
        
        t1=(int) System.currentTimeMillis();
        for(int i=0;i<=a;i++)
            if(NguyenTo(i)){
                count++;
              // System.out.print(i+" ");
            }
        t2=(int) System.currentTimeMillis();
        System.out.print("\nSo cac so nguyen to nho hon 8tr la: " +count);
        System.out.print("\nthoi gian tinh la " + (t2-t1)+"\n");
        /*
        for(int i=0;i<a;i++)
            m[i]=(int)(Math.random()*1000000);
        t1=    (int) System.currentTimeMillis();
        sort(m,a);
        t2= (int)System.currentTimeMillis();
        search(m,a,x);
        t3=(int)System.currentTimeMillis();
        System.out.print("\nThoi gian XX: " +  (t2-t1));
        System.out.print("\nThoi gian tim kiem: " +  (t3-t2)+"\n");
         */
    }

}
